package com.ai.bitcoin.jpa.dao;

import com.ai.bitcoin.constant.Bcc;
import com.ai.bitcoin.jpa.entity.BccDailyInfoHis;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author Jason
 * @date 2018/4/4
 **/
public interface BccDailyInfoHisDao extends JpaRepository<BccDailyInfoHis, String> {
    List<BccDailyInfoHis> findByBccIdAndBatchNo(String bccId, String batchNo);

    List<BccDailyInfoHis> findByBccIdAndRecordState(String bccId, String recordState);

    @Query(nativeQuery = true,value = "SELECT * FROM bcc_daily_info_his WHERE bcc_id =?1 AND create_time >DATE_SUB(curdate(),INTERVAL 1 DAY) AND create_time < DATE_SUB(curdate(),INTERVAL 0 DAY)")
    List<BccDailyInfoHis> findByBccIdYesterday(String bccId);

    @Query(nativeQuery = true, value = "update bcc_daily_info_his set record_state ='" + Bcc.RecordState.PUBLISH + "' where batch_no = ?1")
    @Modifying
    void update2Publish(String unPublishBatch);

    @Query(nativeQuery = true, value = "delete  from bcc_daily_info_his where record_state ='" + Bcc.RecordState.UN_PUBLISH + "'")
    @Modifying
    void deleteNoPublish();

    @Query(nativeQuery = true, value = "delete  from bcc_daily_info_his where batch_no<> ?1 or record_state ='" + Bcc.RecordState.PUBLISH + "'")
    @Modifying
    void deleteOldPublish(String publishBatch);

    List<BccDailyInfoHis> findByBccId(String bccId);
}
